package com.itboye.ihomebank.activity.login;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.broad.RxDialogOne;
import com.itboye.ihomebank.util.dialog.RxDialog;
import com.itboye.ihomebank.widget.VerificationCodeView;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/3/7 0007.
 */

public class AddDeviceActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    ImageView close_icon;//返回
    TextView add_lift_tv;

    static int DeviceResultCode=11;
    Intent loginIntent;
    Button verifyBtn;
    TextView tv_remind;
    UserPresenter userPresenter;
    String phone;

    @Override
    protected int layout_id() {
        return R.layout.activity_addnewadvice;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_lift_tv.setText("返回");
        loginIntent=getIntent();
        phone=loginIntent.getStringExtra("phone");
        tv_remind.setText("由于你再新设备上的登录，需要验证你的手机号("+phone+")");
        userPresenter=new UserPresenter(AddDeviceActivity.this);

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){

            case R.id.add_lift_tv:
            case R.id.close_icon:
            loginIntent.putExtra("data",1);
            setResult(DeviceResultCode,loginIntent);
                this.finish();
                break;

            case R.id.verifyBtn:
            userPresenter.send(phone,"16");
                break;


        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity!=null){

            if (entity.getEventType() == UserPresenter.send_success){
                ByAlert.alert("验证码发送成功");
                final VerifyDialog verifyDialog = new VerifyDialog(AddDeviceActivity.this);
                 verifyDialog.show();

                    verifyDialog.getTv_sure().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (verifyDialog.getSmsEt().getInputContent().length()>5){
                                verifyDialog.cancel();
                                userPresenter.verifyBySms(phone,verifyDialog.getSmsEt().getInputContent().toString().trim());
                            }else {
                                ByAlert.alert("请输入验证码");
                            }

                        }
                    });
                    verifyDialog.getTv_cancel().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            verifyDialog.cancel();
                        }
                    });

            }else if (entity.getEventType() == UserPresenter.send_fail){
                ByAlert.alert(entity.getMsg());
            }else if (entity.getEventType() == UserPresenter.deviceloginSms_success){
                loginIntent.putExtra("data",0);
                setResult(DeviceResultCode,loginIntent);
                AddDeviceActivity.this.finish();
            }else if (entity.getEventType() == UserPresenter.deviceloginSms_fail){
                ByAlert.alert("验证码验证失败");
                loginIntent.putExtra("data",1);
                setResult(DeviceResultCode,loginIntent);
            }
        }
    }

    class VerifyDialog extends RxDialog {
        VerificationCodeView icv;
        TextView tv_sure,tv_cancel;

        public VerificationCodeView getSmsEt() {
            return icv;
        }

        public void setSmsEt(VerificationCodeView smsEt) {
            this.icv = smsEt;
        }

        public TextView getTv_sure() {
            return tv_sure;
        }

        public TextView getTv_cancel() {
            return tv_cancel;
        }

        public void setTv_cancel(TextView tv_cancel) {
            this.tv_cancel = tv_cancel;
        }

        public void setTv_sure(TextView tv_sure) {
            this.tv_sure = tv_sure;
        }

        public VerifyDialog(Context context, int themeResId) {
            super(context, themeResId);
            initView();
        }

        public VerifyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            initView();
        }

        public VerifyDialog(Context context) {
            super(context);
            initView();
            this.setCanceledOnTouchOutside(false);
            this.setCancelable(false);
        }

        public VerifyDialog(Context context, float alpha, int gravity) {
            super(context, alpha, gravity);
            initView();
        }

        private void initView() {
            View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_verify_sms, null);
            icv = (VerificationCodeView) dialogView.findViewById(R.id.icv);
            tv_sure = (TextView) dialogView.findViewById(R.id.tv_sure);
            tv_cancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
            setContentView(dialogView);
//            dialogView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Log.d("=====","====");
//                }
//            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    VerifyDialog.this.cancel();
                }
            });




        }
    }


}
